import logging

from agent.common.system import listDevice, sysCommand
from agent.feature.base import Feature

logger = logging.getLogger('common')

class Elevator(Feature):
    __domain__ = "disk" 
    __feature__ = "elevator"
    
    def __init__(self):
        super().__init__(self.__domain__, self.__feature__)
        
        self.devices = listDevice()
        if self.devices.__len__() == 0:
            logger.error("No device is avaliable in this environment.")
            raise Exception("No device is avaliable in this environment.")
        
        logger.info("elevator use device list: {}".format(self.devices))
    
    def set_value(self, value):
        if type(value) is not list:
            value = [value, ] * len(self.devices)
            
        if len(value) != len(self.devices):
            logger.warning("size unmatch: {} != {}".format(len(value), len(self.devices)))
        
        for val, dev in zip(value, self.devices):
            sysCommand("echo {} > /sys/block/{}/queue/scheduler".format(val, dev))

    def get_value(self):
        values = []
        for dev in self.devices:
            values.append(sysCommand("cat /sys/block/{dev}/queue/scheduler".format(dev = dev)))
        return values